import localStorageUtil from './localstorage'
import cookieUtil from './cookie.js'

export const saveInfo = (info) => {
  var loginExpired = new Date().getTime() + 1000 * 60 + 60
  info.logined = true
  if (localStorageUtil.available) {
    localStorageUtil.setItem('loginInfo', info)
    localStorageUtil.setItem('loginExpired', loginExpired)
  } else {
    cookieUtil.setItem('loginInfo', JSON.stringify(info))
    cookieUtil.setItem('loginExpired', loginExpired)
  }
}

export const setExpired = () => {
  var expiredTime = new Date().getTime() + 1000 * 60 * 60
  if (localStorageUtil.available) {
    localStorageUtil.setItem('loginExpired', expiredTime)
  } else {
    cookieUtil.setItem('loginExpired', expiredTime)
  }
}
export const getExpired = () => {
  if (localStorageUtil.available) {
    return localStorageUtil.getItem('loginExpired')
  } else {
    return JSON.parse(cookieUtil.getItem('loginExpired') || '0')
  }
}

export const removeInfo = () => {
  if (localStorageUtil.available) {
    localStorageUtil.removeItem('loginInfo')
  } else {
    cookieUtil.removeItem('loginInfo')
  }
}

export const getInfo = () => {
  let loginInfo = null
  if (localStorageUtil.available) {
    loginInfo = localStorageUtil.getItem('loginInfo')
  } else if (cookieUtil.getItem('loginInfo')) {
    loginInfo = JSON.parse(cookieUtil.getItem('loginInfo'))
  }
  return loginInfo
}
